<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\fkshou\model;

use app\fkshou\common\Jwts;
use think\Model as ThinkModel;

/**
 * 授权模型
 * @package app\fkshou\model
 */
class Register extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'fkshou_register';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 定义修改器
    public function setStartTimeAttr($value)
    {
        return $value != '' ? strtotime($value) : 0;
    }
    public function setEndTimeAttr($value)
    {
        return $value != '' ? strtotime($value) : 0;
    }
    public function getStartTimeAttr($value)
    {
        return $value != 0 ? date('Y-m-d', $value) : '';
    }
    public function getEndTimeAttr($value)
    {
        return $value != 0 ? date('Y-m-d', $value) : '';
    }


    static public function create_token($data)
    {
        $user_code = Jwts::getInstance()->CheckJwt($data['user_code']);
        if (!$user_code) {
            return false;
        }
        if (!isset($user_code['CpuId'])) {
            dump($user_code);
            return false;
        }
        $db_user_code = self::where(['wx_id' => $data['wx_id'], 'phone' => $data['phone']])->value('user_code');
        if ($data['status'] == true && !$db_user_code) {
            $payload = [
                'CpuId' => $user_code['CpuId'],
                'register' => $data['typeid'],
                'wx_id' => $data['wx_id'],
                'phone' => $data['phone'],
                'create_time' => time(),
                'auth_hash' => md5($data['user_code'])
            ];
            $jwt = Jwts::getInstance()->Create($payload);
            $data['auth_code'] = $jwt;
            self::create($data);
            return true;
        }
        return false;
    }

}
